import axios from "axios";
const request = axios.create({
    baseURL: "/api",
    timeout: 500000,
});
request.interceptors.request.use((config) => {
    let token = window.localStorage.vuex ? JSON.parse(window.localStorage.vuex).user.token : '';
    config.headers.Authorization = token;
    debugger
    return config;
});
request.interceptors.response.use(
    (res) => {
        return res.data;
    },
    (error) => {
        if (error && error.response) {
            switch (error.response.status) {
                case 401:
                    error.message = "未授权，请重新登录";
                    break;
                case 403:
                    error.message = "拒绝访问";
                    break;
                case 404:
                    error.message = "请求错误,未找到该资源";
                    window.location.href = "/NotFound";
                    break;
                case 405:
                    error.message = "请求方法未允许";
                    break;
                case 500:
                    error.message = "服务器端出错";
                    break;
                default:
                    error.message = `连接错误${error.response.status}`;
            }
        }
        // 超时处理
        // else if (JSON.stringify(error).includes("timeout")) {
        //   Message.error("服务器响应超时，请刷新当前页");
        //   error.message = "连接服务器失败";
        //   Message.error(error.message);
        // }
    }
);
export default request;